
package net.swa.system.web.action;

import java.util.Collection;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.swa.system.beans.entity.Dict;
import net.swa.system.beans.entity.User;
import net.swa.system.service.ICommonService;
import net.swa.system.util.SysConst;
import net.swa.util.JsonResult;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public abstract class AbstractBaseAction extends ActionSupport
{
    private static final long serialVersionUID = 6754815913796667235L;

    protected String contextPath;

    protected int currentPage;

    protected int pageSize;

    protected int totalCount;

    protected String orderBy;

    protected String orderType;

    public AbstractBaseAction()
    {
        contextPath = getSession().getServletContext().getContextPath();
    }

    /**
     * 查询字典,先从session中找,如果找到,直接返回,否则从数据库中查询,然后放到session中<br/>
     * 
     * @param dictName
     *            字典名
     * @param commonService
     *            由调用者传入
     * @return 字典集合
     */
    @SuppressWarnings("unchecked")
    protected List<Dict> getDict(String dictName , ICommonService commonService)
    {
        ServletContext context = getRequest().getSession().getServletContext();
        List<Dict> dicts = (List<Dict>) context.getAttribute(dictName);
        if (dicts == null)
        {
            dicts = commonService.search("title", dictName, Dict.class);
            context.setAttribute(dictName, dicts);
        }
        return dicts;
    }

    protected void outError(String msg) throws Exception
    {
        JsonResult<Object> json = new JsonResult<Object>();
        json.setSuccess(false);
        json.setMessage(msg);
        outJson(json);
    }

    protected void outSuccess() throws Exception
    {
        JsonResult<Object> json = new JsonResult<Object>();
        outJson(json);
    }

    protected HttpServletRequest getRequest()
    {
        return ServletActionContext.getRequest();
    }

    protected HttpServletResponse getResponse()
    {
        return ServletActionContext.getResponse();
    }

    protected HttpSession getSession()
    {
        return getRequest().getSession();
    }

    @Deprecated
    public HttpSession getSession(boolean create)
    {
        return ServletActionContext.getRequest().getSession(create);
    }

    public void setCurrentUser(User user)
    {
        setSessionAttribute(SysConst.CURRENT_USER, user);
    }

    public User getCurrentUser()
    {
        User user = (User) getSessionAttribute(SysConst.CURRENT_USER);
        return user;
    }

    protected void setSessionAttribute(String key , Object obj)
    {
        getSession().setAttribute(key, obj);
    }

    protected Object getSessionAttribute(String key)
    {
        return getSession().getAttribute(key);
    }

    protected void removeSessionAttribute(String key)
    {
        getRequest().getSession().removeAttribute(key);
    }

    protected String getRequestParameter(String key)
    {
        return getRequest().getParameter(key);
    }

    protected Object getRequestAttribute(String key)
    {
        return getRequest().getAttribute(key);
    }

    protected void setRequestAttribute(String key , Object obj)
    {
        getRequest().setAttribute(key, obj);
    }

    protected void outString(String obj) throws Exception
    {
        HttpServletResponse response = getResponse();
        response.setContentType("text/plain");
        response.setCharacterEncoding("utf-8");
        response.getWriter().println(obj);
        response.getWriter().flush();
    }

    public void outJson(Object obj) throws Exception
    {
        HttpServletResponse response = getResponse();
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");

        String json = null;
        if (obj instanceof Collection<?>)
        {
            JSONArray ja = JSONArray.fromObject(obj);
            json = ja.toString();
        }
        else
        {
            json = JSONObject.fromObject(obj).toString();
        }
        response.getWriter().println(json);
        response.getWriter().flush();
    }

    public String getJson(Object obj)
    {
        String json = null;
        if (obj instanceof Collection<?>)
        {
            JSONArray ja = JSONArray.fromObject(obj);
            json = ja.toString();
        }
        else
        {
            json = JSONObject.fromObject(obj).toString();
        }
        return json;
    }

    public String getContextPath()
    {
        return contextPath;
    }

    public void setContextPath(String contextPath)
    {
        this.contextPath = contextPath;
    }

    public int getCurrentPage()
    {
        return currentPage;
    }

    public void setCurrentPage(int currentPage)
    {
        this.currentPage = currentPage;
    }

    public int getPageSize()
    {
        return pageSize;
    }

    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }

    public int getTotalCount()
    {
        return totalCount;
    }

    public void setTotalCount(int totalCount)
    {
        this.totalCount = totalCount;
    }

    public String getOrderBy()
    {
        return orderBy;
    }

    public void setOrderBy(String orderBy)
    {
        this.orderBy = orderBy;
    }

    public String getOrderType()
    {
        return orderType;
    }

    public void setOrderType(String orderType)
    {
        this.orderType = orderType;
    }

}
